import { isProd } from './env';
const CompressionPlugin = require('compression-webpack-plugin');

export default {
  chunks: isProd ? ['vendors', 'antd', 'echart', 'lodash', 'umi'] : ['umi'],
  chainWebpack: function (config: any, {}) {
    config.module
      .rule('mp4')
      .test(/\.(mp4|zip)(\?.*)?$/)
      .use(require('file-loader'))
      .loader('file-loader')
      .options({
        name: 'static/[name].[hash:8].[ext]',
      });
    config.module
      .rule('pdf')
      .test(/\.(pdf)(\?.*)?$/)
      .use(require('file-loader'))
      .loader('file-loader')
      .options({
        name: 'static/[name].[hash:8].[ext]',
      });
    if (isProd) {
      // 压缩代码
      config.plugin('compression-webpack-plugin').use(CompressionPlugin, [
        {
          test: /\.(js|css|html)$/i, // 匹配
          threshold: 10240, // 超过10k的文件压缩
          deleteOriginalAssets: false, // 不删除源文件
        },
      ]);
      config.merge({
        optimization: {
          splitChunks: {
            chunks: 'all',
            minSize: 20000,
            minChunks: 2,
            cacheGroups: {
              lodash: {
                chunks: 'all',
                name: 'lodash',
                test: /lodash/,
                priority: 10,
              },
              antd: {
                name: 'antd',
                chunks: 'all',
                test: /(@antd|antd|@ant-design)/,
                priority: 9,
              },
              echarts: {
                name: 'echarts',
                test: /echarts/,
                chunks: 'all',
                priority: 8,
              },
              umi: {
                chunks: 'all',
                name: 'umi',
                test: /[\\/]node_modules[\\/]/,
                priority: 7,
              },
              vendors: {
                chunks: 'all',
                name: 'vendors',
                test: /[\\/]node_modules[\\/]/,
                priority: 6,
              },
            },
          },
        },
      });
    }
  },
};
